package org.jsola.hr.service.impl;

import org.jsola.common.DateKit8;
import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IWeekdaysDAO;
import org.jsola.hr.dto.WeekdaysAddDTO;
import org.jsola.hr.dto.WeekdaysUpdateDTO;
import org.jsola.hr.entity.WeekdaysDO;
import org.jsola.hr.query.WeekdaysQuery;
import org.jsola.hr.service.IWeekdaysService;
import org.jsola.hr.vo.WeekdaysListVO;
import org.jsola.hr.vo.WeekdaysVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 工作日历表
 *
 * @author zhr
 */
@Service("hrWeekdaysServiceImpl")
public class WeekdaysServiceImpl implements IWeekdaysService {

    @Autowired
    private IWeekdaysDAO weekdaysDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public WeekdaysVO save(WeekdaysAddDTO weekdaysAddDTO, TokenUser tokenUser) {
        // 转DO
        WeekdaysDO weekdaysDO = weekdaysAddDTO.to(WeekdaysDO.class);
        // 保存
        weekdaysDO = save(weekdaysDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return weekdaysDO.to(WeekdaysVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(WeekdaysUpdateDTO weekdaysUpdateDTO, TokenUser tokenUser) {
        //转DO
        WeekdaysDO weekdaysDO = weekdaysUpdateDTO.to(WeekdaysDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(weekdaysDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... weekdaysIds) {
        return deleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) weekdaysIds);
    }


    @Override
    public WeekdaysVO selectById(String weekdaysId, String siteId) {
        WeekdaysDO weekdaysDO = selectDOById(weekdaysId, siteId);
        if (weekdaysDO == null) {
            return null;
        }
        return weekdaysDO.to(WeekdaysVO.class);
    }

    @Override
    public List<WeekdaysListVO> select(WeekdaysQuery weekdaysQuery, String siteId) {
        List<WeekdaysDO> weekdaysDOList = selectDO(weekdaysQuery, siteId);
        if(CollectionUtils.isEmpty(weekdaysDOList)) {
            return weekdaysDOList == null ? null : new ArrayList<>();
        }
        return weekdaysDOList.stream()
                .map(weekdaysDO -> weekdaysDO.to(WeekdaysListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(WeekdaysQuery weekdaysQuery, String siteId) {
        Example example = buildExample(weekdaysQuery,siteId);
        return weekdaysDAO.selectCountByExample(example);
    }

    @Override
    public Page<WeekdaysListVO> selectPage(WeekdaysQuery weekdaysQuery, String siteId) {
        Example example = buildExample(weekdaysQuery,siteId);
        Page<WeekdaysDO> page = weekdaysDAO.selectPageByExample(example,
                weekdaysQuery.getPageNo(),
                weekdaysQuery.getPageSize());

        return page.to(WeekdaysListVO.class);
    }

    @Override
    public WeekdaysDO selectDOById(String weekdaysId, String siteId) {
        return listById(weekdaysId, siteId);
    }

    @Override
    public List<WeekdaysDO> selectDO(WeekdaysQuery weekdaysQuery, String siteId) {
        Example example = buildExample(weekdaysQuery,siteId);
        return weekdaysDAO.selectByExample(example);
    }


    @Override
    public int selectCountWeekdays(String startTime, String endTime) {
        return weekdaysDAO.selectCountWeekday(startTime,endTime);
    }

    @Override
    public int workdayListNotIncludeEndDate(Date startTime, Date endTime) {
        return weekdaysDAO.workdayListNotIncludeEndDate(DateKit8.getStartTime(startTime), DateKit8.getEndTime(endTime));
    }

    @Override
    public int workdayListIncludeEndDate(Date startTime, Date endTime) {
        return weekdaysDAO.workdayListIncludeEndDate(DateKit8.getStartTime(startTime), DateKit8.getEndTime(endTime));
    }

    /**
     * 根据查询参数，构建example

     * @param weekdaysQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(WeekdaysQuery weekdaysQuery, String siteId) {
        Example example = new Example(WeekdaysDO.class);
        example.and()
                .andEqualTo("siteId", siteId);
        if (weekdaysQuery.getHolidayDateStart() != null &&
                weekdaysQuery.getHolidayDateEnd() != null) {
            example.and().andBetween("holidayDate", weekdaysQuery.getHolidayDateStart(),
                    weekdaysQuery.getHolidayDateEnd());
        }
        if (weekdaysQuery.getWeekday() != null) {
            example.and().andEqualTo("weekday", weekdaysQuery.getWeekday());
        }
        if (weekdaysQuery.getHoliday() != null) {
            example.and().andEqualTo("holiday", weekdaysQuery.getHoliday());
        }
        if (weekdaysQuery.getStatus() != null) {
            example.and().andEqualTo("status", weekdaysQuery.getStatus());
        }
        // 排序
        ExampleKit.setExampleOrder(example,weekdaysQuery.getOrders());
        return example;
    }
}




